package com.aramis.mydytt.db

import android.content.Context
import androidx.room.*
import com.aramis.mydytt.ui.bean.MovieItemBean
import io.reactivex.Observable
import java.io.Serializable

/**
 * Description: AppDatabase
 * @author lizhidan
 * Created: 2021/6/4
 */
@Entity(tableName = "movies_favorite")
class FavoriteMovieBean : Serializable {
    @PrimaryKey(autoGenerate = true)
    var id: Long = 0
    var movieId: String = ""
    var createTime: Long = 0
    var title: String = ""
    var path: String? = null
    var score: String? = null
    var playSubTitle: String? = null

    fun toMovieItemBean(): MovieItemBean {
        return MovieItemBean(movieId, title, path ?: "", score, null, 0, playSubTitle)
    }
}

@Entity(tableName = "movies_downloaded")
class DownloadedMovieBean : Serializable {

    @PrimaryKey(autoGenerate = true)
    var id: Long = 0
    var movieId: String = ""
    var createTime: Long = 0
    var title: String = ""
    var path: String? = null
    var score: String? = null
    var playSubTitle: String? = null

    fun toMovieItemBean(): MovieItemBean {
        return MovieItemBean(movieId, title, path ?: "", score, null, 0, playSubTitle)
    }
}

@Database(version = 1, entities = [FavoriteMovieBean::class, DownloadedMovieBean::class])
abstract class AppDatabase : RoomDatabase() {
    abstract fun favoriteMovieDao(): FavoriteMovieDao?

    abstract fun downloadedMovieDao(): DownloadedMovieDao?
}

@Dao
interface FavoriteMovieDao {

    @Query("SELECT * FROM movies_favorite WHERE movieId=:movieId")
    fun selectByMovieId(movieId: String): Observable<FavoriteMovieBean?>

    @Query("SELECT * FROM movies_favorite;")
    fun selectAll(): List<FavoriteMovieBean>

    @Query("SELECT * FROM movies_favorite;")
    fun selectAllO(): Observable<List<FavoriteMovieBean>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(bean: FavoriteMovieBean): Long

    @Delete
    fun delete(bean: FavoriteMovieBean)

    @Query("DELETE FROM movies_favorite")
    fun deleteAll()

    @Query("DELETE FROM movies_favorite WHERE id = :favoriteMovieId")
    fun deleteById(favoriteMovieId: Long)

    @Query("DELETE FROM movies_favorite WHERE movieId = :movieId")
    fun deleteByMovieId(movieId: String)
}

@Dao
interface DownloadedMovieDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(bean: DownloadedMovieBean): Long

    @Query("SELECT * FROM movies_downloaded WHERE movieId=:movieId")
    fun selectByMovieId(movieId: String): Observable<DownloadedMovieBean?>

    @Query("SELECT * FROM movies_downloaded;")
    fun selectAllO(): Observable<List<DownloadedMovieBean>>

    @Query("SELECT * FROM movies_downloaded;")
    fun selectAll(): List<DownloadedMovieBean>

    @Query("DELETE FROM movies_downloaded")
    fun deleteAll()

    @Query("DELETE FROM movies_downloaded WHERE movieId = :movieId")
    fun deleteByMovieId(movieId: String)

}

object DBManager {
    var db: AppDatabase? = null

    fun init(context: Context) {
        db = Room.databaseBuilder(context, AppDatabase::class.java, "my_dytt_db").build()
    }

    fun close() {
        db?.close()
    }
}